home *** CD-ROM | disk | FTP | other *** search
- page 132,63,1,1
- opt rc
- title 'HDLC protocol handling'
-
- ;***************************************************************
- ;* HDLC.ASM -- HDLC protocol handling *
- ;* *
- ;* Provides a HDLC low-level protocol handling: *
- ;* flag generation/detecting, CRC calculation/checking, *
- ;* zero insertion/deletion and data serializer. *
- ;* *
- ;* HDLC protocol handling is based on article *
- ;* Carlson, D., E.: *
- ;* "Bit-Oriented Data Link Control Procedures", *
- ;* IEEE Trans. on Comm., Vol. 28, No. 4, April 1980 *
- ;* CRC calculation/checking is based on article *
- ;* Morse, G.: *
- ;* "Calculating CRCs by bits and bytes", *
- ;* BYTE Vol. 11, No. 9, September 1986 *
- ;* *
- ;* This module uses registers as follows: *
- ;* r0 - general purpose *
- ;* *
- ;* Copyright (C) 1990, 1991 by Alef Null. All rights reserved. *
- ;* Author(s): Jarkko Vuori, OH2LNS *
- ;* Modification(s): *
- ;***************************************************************
-
- section HDLC
- xref rdhost,tsthost
- xref wrhost,endfrm,rejfrm
-
- xdef HDLC_i
- xdef getbit,putbit
-
- ; HDLC equations
- flagmsk equ $ff ; left justified flag mask
- flag equ %01111110 ; HDLC bit flag
- abrtmsk equ $fe ; left justified abort mask
- abort equ %11111110 ; abort sequence
- fivemsk equ $fe ; left justified five bit mask
- five equ %01111100 ; left justified five bit sequence
- poly equ $8408 ; HDLC CRC polynomial (x^16 + x^12 + x^5 + 1)
- crcchk equ $f0b8 ; special CRC checkword
-
- ; Xmitter status flags
- ztstflg equ 0
- zinsflg equ 1
-
- ; Receiver status flags
- hunt equ 0
- firstb equ 1
- scndb equ 2
-
- org p:
-
- nolist
- include 'macros'
- list
-
- ; CRC calculation routine
- ; data in the LSB of a
- crc macro rem
- move #>1,x0
- and x0,a y:<rem,x0
- eor x0,a
- lsr a #>poly,x0
- jcc <_crc1
- eor x0,a
- _crc1 move a1,y:<rem
- endm
-
-
- ;****************************
- ;* Initialize HDLC handling *
- ;****************************
- HDLC_i
- ; initialize coder
- movi xstD,y:<xstate
- movi flag,y:<xdata
- movi $0,y:<x5bit
- movi 0,y:<xbit
- movi $0,y:<xstatus ; disable 11111 check
- ; initialize decoder
- movi 0,y:<rdata
- movi $0,y:<rflag
- movi $7,y:<rstatus ; hunt mode
-
- rts
-
-
- ;****************************
- ;* Get a bit *
- ;****************************
- ; returns next bit to be sent in C
- ; returns Z if this is an end of the transmission
- ; Note! Interrupts are disabled if end of transmission detected
- getbit
- ; check if we must insert a zero
- bclr #zinsflg,y:<xstatus
- jcs <getins
- ; check if there are bits left
- move y:<xbit,a
- tst a y:<xstate,r0
- nop
- jeq (r0)
- ; five bit sequence detection logic
- getsft0 jclr #ztstflg,y:<xstatus,getsft1 ; check if logic enabled
- move y:<xdata,a
- lsr a
- move y:<x5bit,a
- ror a #>$f80000,x0
- and x0,a
- cmp x0,a a1,y:<x5bit
- jne <getsft1
- ; 11111 detected, insert zero
- bset #zinsflg,y:<xstatus
- ; calculate CRC
- getsft1 move y:<xdata,a
- crc xcrcrem
- ; shift data out (LSB first) and decrement bit counter
- move y:<xbit,r0
- move y:<xdata,a
- lsr a (r0)-
- move r0,y:<xbit
- move a1,y:<xdata
- andi #$fb,ccr ; NZ
- rts
- ; insert zero bit
- getins clr a ; reset five bit counter
- move a1,y:<x5bit
- andi #$fa,ccr ; NC NZ
- rts
-
-
- ; --- A, after a begin flag
- xstA
- ; set up data xmission
- movi xstB,y:<xstate
- movi $00ffff,y:<xcrcrem ; init CRC generator
- movi $1,y:<xstatus ; enable 11111 checker
-
- ; --- B, after data byte sent
- xstB movi 8,y:<xbit ; init bit counter for the next byte
- jsr <rdhost ; fetch next byte
- move x0,y:<xdata
- jpl <getsft0
- ; last databyte sent, send CRC
- movi xstC,y:<xstate
- movi 16,y:<xbit
- move y:<xcrcrem,a
- not a
- move a1,y:<xdata
- jmp <getsft0
-
- ; --- C, after CRC sent
- xstC movi xstD,y:<xstate
- movi flag,y:<xdata
- movi 8,y:<xbit
- movi $0,y:<xstatus ; disable 11111 checker
- jmp <getsft0
-
- ; --- D, after the last flag sent
- xstD ori #$02,mr
- jsr <tsthost
- jeq <xstD1
- ; new data to send, start a new frame
- andi #$fc,mr
- movi xstA,y:<xstate
- movi flag,y:<xdata
- movi 8,y:<xbit
- jmp <getsft0
- rts
- ; no new data, return with Z
- xstD1 rts
-
-
- ;****************************
- ;* Put a bit *
- ;****************************
- ; put next bit in C to the host transmit queue
- putbit
- move y:<rflag,a
- ror a #abrtmsk,b
- move a1,y:<rflag
- move a1,x0
- ; check if abort sequence detected
- and x0,b #abort,y0
- eor y0,b
- jeq <putb4
- ; check if flag detected
- move #flagmsk,b
- and x0,b #flag,y0
- eor y0,b #fivemsk,a
- jeq <putb3 ; yes, special handling
- ; check if 11111 sequence detected
- and x0,a #five,y0
- eor y0,a x0,b
- jeq <putb2 ; yes, ignore this bit
- ; no special sequence detected, shift data normally
- jset #hunt,y:<rstatus,putb2
- lsl b y:<rdata,a
- ror a #>@pow(2,-15),x1
- move a1,y:<rdata
- move a1,x0
- ; calculate CRC
- mpy x0,x1,a #>1,x1 ; shift to right 15 bits
- crc rcrcrem
- ; decrement the bit counter
- move y:<rbit,a
- sub x1,a #8,r0
- move a,y:<rbit
- jne <putb2
- ; 8 bit shifted, init bit counter again
- move r0,y:<rbit
- bclr #firstb,y:<rstatus
- jcc <putb1
- ; first byte, init CRC checker
- move #$00ffff,a1
- move a1,y:<rcrcrem
- rts
- ; data bytes, put it to the queue
- putb1 bclr #scndb,y:<rstatus
- jcs <putb2
- move y:<rdata,a
- move #>$ff,x0
- and x0,a
- move a1,x0
- jmp <wrhost
- ; discard the previous bit
- putb2 rts
- ; flag detected
- putb3 bclr #hunt,y:<rstatus
- movi 8,y:<rbit
- bset #firstb,y:<rstatus
- bset #scndb,y:<rstatus
- jcs <rejfrm ; reject frame if it is too short
- ; calculate the last CRC bit
- move y:<rdata,x0
- move #>@pow(2,-16),x1 ; shift to right 16 bits
- mpy x0,x1,a
- crc rcrcrem
- ; check that it is valid
- move #>crcchk,x0
- eor x0,a
- jeq <endfrm
- jmp <rejfrm ; reject frame if CRC failed
- ; abort detected
- putb4 bset #hunt,y:<rstatus
- jmp <rejfrm
-
-
- ;****************************
- ;* DATA - AREA *
- ;****************************
-
- org y:
-
- ; coder
- xstate ds 1 ; current xmitter state
- xdata ds 1 ; current byte to be send
- x5bit ds 1 ; one bit counter
- xstatus ds 1 ; current xmitter status
- xbit ds 1 ; send bits counter
- xcrcrem ds 1 ; CRC remainder
-
- ; decoder
- rdata ds 1 ; current byte received
- rflag ds 1 ; one bit counter
- rstatus ds 1 ; current receiver status
- rbit ds 1 ; received bit counter
- rcrcrem ds 1 ; CRC remainder
-
- endsec
-
- end
-